package biz

import (
	"github.com/go-kratos/kratos/v2/log"
	"mykratos/internal/data"
)

type Cart struct {
	ID        int64 `gorm:"primary_key;not_null;auto_increment" json:"id"`
	ProductID int64 `gorm:"not_null" json:"product_id"`
	Num       int64 `gorm:"not_null" json:"num"`
	SizeID    int64 `gorm:"not_null" json:"size_id"`
	UserID    int64 `gorm:"not_null" json:"user_id"`
}

type ICartDataService interface {
	AddCart(*Cart) (int64, error)
	DeleteCart(int64) error
	UpdateCart(*Cart) error
	FindCartByID(int64) (*Cart, error)
	FindAllCart(int64) ([]Cart, error)

	CleanCart(int64) error
	DecrNum(int64, int64) error
	IncrNum(int64, int64) error
}

func NewCartDataService(repo data.ICartRepository, logger log.Logger) *CartDataService {
	return &CartDataService{CartRepository: repo, log: log.NewHelper(logger)}
}

type CartDataService struct {
	CartRepository data.ICartRepository
	log            *log.Helper
}

//插入
func (u *CartDataService) AddCart(cart *Cart) (int64, error) {
	return u.CartRepository.CreateCart(cart)
}

//删除
func (u *CartDataService) DeleteCart(cartID int64) error {
	return u.CartRepository.DeleteCartByID(cartID)
}

//更新
func (u *CartDataService) UpdateCart(cart *Cart) error {
	return u.CartRepository.UpdateCart(cart)
}

//查找
func (u *CartDataService) FindCartByID(cartID int64) (*Cart, error) {
	return u.CartRepository.FindCartByID(cartID)
}

//查找
func (u *CartDataService) FindAllCart(userID int64) ([]Cart, error) {
	return u.CartRepository.FindAll(userID)
}

func (u *CartDataService) CleanCart(userID int64) error {
	return u.CartRepository.CleanCart(userID)
}

func (u *CartDataService) DecrNum(cartID int64, num int64) error {
	return u.CartRepository.DecrNum(cartID, num)
}

func (u *CartDataService) IncrNum(cartID int64, num int64) error {
	return u.CartRepository.IncrNum(cartID, num)
}
